/**
 * 
 */
package validator;

import java.awt.Color;
import java.awt.Component;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;

/**
 * @author nguyen.nhat.nguyen
 *
 */
public class JNumberValidator extends JLabel {

	private boolean _checkValidNumber;
    private boolean _checkMax;
    private boolean _checkMin;

    private int _max;
    private int _min;
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * 
	 */
	public JNumberValidator() {
                super();
	}
	
	public void setCheckMode(boolean checkValidNumber, boolean checkMax, boolean checkMin)
	{
		this._checkValidNumber = checkValidNumber;
		this._checkMax = checkMax;
		this._checkMin = checkMin;
	}
	
	private boolean checkValidNumber()
	{
		Component obj = this.getLabelFor();
		boolean isValid = false;

		String numberPattern = "^[0-9]+\\.?[0-9]+$";
		Pattern pattern = Pattern.compile(numberPattern);
		Matcher matcher;

		if(obj instanceof JTextField)
		{
	        matcher = pattern.matcher(((JTextField)obj).getText());
	        if(matcher.matches())
	        {
	        	isValid = true;
	        }
		}

		if(obj instanceof JTextPane)
		{
			matcher = pattern.matcher(((JTextPane)obj).getText());
			if(matcher.matches())
	        {
	        	isValid = true;
	        }
		}

		if(obj instanceof JTextArea)
		{
			matcher = pattern.matcher(((JTextArea)obj).getText());
			if(matcher.matches())
	        {
	        	isValid = true;
	        }
		}
		
		if(!isValid)
        {
        	this.setText(ValidatorConst.VALIDATOR_NUMBER_VALID_MESSAGE);
        	obj.setBackground(Color.GREEN);
        }

		return isValid;
	}

	private boolean checkMaxNumber()
	{
		Component obj = this.getLabelFor();
        boolean isValid = true;

        if(obj instanceof JTextField)
        {
                if(Integer.parseInt(((JTextField)obj).getText()) > this._max)
                {
                        isValid = false;
                }
        }
        if(obj instanceof JTextArea)
        {
                if(Integer.parseInt(((JTextArea)obj).getText()) > this._max)
                {
                        isValid = false;
                }
        }
        if(obj instanceof JTextPane)
        {
                if(Integer.parseInt(((JTextPane)obj).getText()) > this._max)
                {
                        isValid = false;
                }
        }

        if(!isValid)
        {
        	this.setText(ValidatorConst.VALIDATOR_NUMBER_MAX_MESSAGE);
        	obj.setBackground(Color.GREEN);
        }
        this.setVisible(!isValid);
        return isValid;
	}
	
	private boolean checkMinNumber()
	{
		Component obj = this.getLabelFor();
        boolean isValid = true;

        if(obj instanceof JTextField)
        {
                if(Integer.parseInt(((JTextField)obj).getText()) < this._min)
                {
                        isValid = false;
                }
        }
        if(obj instanceof JTextArea)
        {
                if(Integer.parseInt(((JTextArea)obj).getText()) < this._min)
                {
                        isValid = false;
                }
        }
        if(obj instanceof JTextPane)
        {
                if(Integer.parseInt(((JTextPane)obj).getText()) < this._min)
                {
                        isValid = false;
                }
        }

        if(!isValid)
        {
        	this.setText(ValidatorConst.VALIDATOR_NUMBER_MIN_MESSAGE);
        	obj.setBackground(Color.GREEN);
        }
        this.setVisible(!isValid);
        return isValid;
	}

	public boolean check()
    {
        boolean isValid = true;

        if(this._checkValidNumber)
        {
            isValid = isValid & this.checkValidNumber();
        }
        else
        {
            if(this._checkMax)
            {
                isValid = isValid & this.checkMaxNumber();
            }
            if(this._checkMin)
            {
                isValid = isValid & this.checkMinNumber();
            }
        }

        return isValid;
    }
}
